import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)


//引入组件（懒加载）
const Layout = () => import('@/layout/index.vue')
const Not_found = () => import('@/views/404.vue')
//const Unauthorized = () => import('@/views/401.vue')

//静态路由
export const constantRouterMap = [
    { 
        path: '/',  component: Layout, redirect: '/dashboard',
        children: [
            {
                path: 'dashboard', name: 'dashboard', component: () => import('@/views/dashboard/index.vue'),
                meta: {
                    
                },
            }
        ]
    },
    { 
        path: '/login', name: 'login', component: () => import('@/views/login/index.vue'),
        meta: {
            needLogin: false,//不需要登陆状态的页面需要手动设置
        }
    }
]


//创建路由
const router = new VueRouter({
    routes: [
        ...constantRouterMap,
    ]
})

//路由守卫
router.beforeEach((to,from,next) =>{
    //判断即将进入的路由是否需要登陆状态
    if(to.meta.needLogin === false){
        //不需要登陆状态的页面直接跳转路由
        //console.log('不需要登陆状态的页面直接跳转路由')
        next()
    }else if(window.localStorage.getItem('_token') === null){
        //需要登陆的页面先判断有无token 没有则跳转到登录页
        //console.log('页面需要登陆,当前无登录状态，为你跳转到登录页')
        next({name:'login'})
    }else{
        //有token直接跳转
        //console.log('有token直接跳转')
        next()
    }
})



//导出
export default router
